/**
 * Copyright &copy; com.smooth.common
 */
package com.smooth.site.sx.mag.goods.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.smooth.common.site.common.persistence.Page;
import com.smooth.common.site.common.utils.IdGenerater;
import com.smooth.dao.gen.BizGoodsMapper;
import com.smooth.dao.gen.bean.BaseCategory;
import com.smooth.dao.gen.bean.BizGoods;
import com.smooth.dao.gen.bean.BizGoodsExample;
import com.smooth.site.sx.mag.basedata.category.service.BaseBizCategoryService;
import com.smooth.site.sx.mag.goods.enums.GoodsStatus;

/**
 * 商品Service
 * @author bm
 * @version 2019-02-07
 */
@Service
@Transactional(readOnly = true)
public class BizGoodsService{
	Logger log = LoggerFactory.getLogger(BizGoodsService.class);
	@Autowired
	BizGoodsMapper bizGoodsMapper;
	@Autowired
	IdGenerater idGenerater;
	
	public BizGoods get(String no) {
		return bizGoodsMapper.selectByPrimaryKey(no);
	}
	
	public List<BizGoods> findList(BizGoods bizGoods) {
		BizGoodsExample bizGoodsExample = new BizGoodsExample();
		BizGoodsExample.Criteria criteria = bizGoodsExample.createCriteria();
		
		return bizGoodsMapper.selectByExample(bizGoodsExample);
	}
	
	public Page<BizGoods> findPage(Page<BizGoods> page, BizGoods bizGoods) {
		BizGoodsExample bizGoodsExample = new BizGoodsExample();
		BizGoodsExample.Criteria criteria = bizGoodsExample.createCriteria();
		
		bizGoodsExample.setPage(page);
		
		List<BizGoods> dlist = bizGoodsMapper.selectByExample(bizGoodsExample);
		page.setList(dlist);
		page.setCount(bizGoodsMapper.countByExample(bizGoodsExample));
		
		return page;
	}
	@Autowired
	BaseBizCategoryService baseBizCategoryService;
	@Transactional(readOnly = false)
	public void save(BizGoods bizGoods) {
		if(StringUtils.isNotBlank(bizGoods.getGoodCategoryId())) {
			BaseCategory baseCategory = baseBizCategoryService.queryById(bizGoods.getGoodCategoryId());
			bizGoods.setGoodCategoryName(baseCategory.getCategoryName());
		}
		if(StringUtils.isNotEmpty(bizGoods.getId())&&get(bizGoods.getId())!=null) {
			Date date = new Date();
			bizGoods.setUpdateDate(date);
			int optNum = bizGoodsMapper.updateByPrimaryKeySelective(bizGoods);
			if(optNum==1) {
				log.info("更新 student 数据【"+bizGoods.toString()+"】成功");
			}else {
				log.error("更新 student 数据【"+bizGoods.toString()+"】失败");
			}
		}else {
			Date date = new Date();
			bizGoods.setCreateDate(date);
			bizGoods.setId(idGenerater.genClsNo("bizGoods"));
			bizGoods.setUpdateDate(date);
			bizGoods.setGoodStatus(GoodsStatus.ONLINE.value);
			int optNum =  bizGoodsMapper.insertSelective(bizGoods);
			if(optNum==1) {
				log.info("插入 student 数据【"+bizGoods.toString()+"】成功");
			}else {
				log.error("插入 student 数据【"+bizGoods.toString()+"】失败");
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(BizGoods bizGoods) {
		bizGoodsMapper.deleteByPrimaryKey(bizGoods.getId());
	}
	
}